package com.open.capacity.db.config;


import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.open.capacity.db.config.util.DataSourceKey;
import com.open.capacity.db.config.util.DynamicDataSource;


/**
 * @author 作者 ftquan E-mail: 464725602@qq.com
 * @version 创建时间：2017年04月23日 下午20:01:06 类说明
 * central-platform
 * 在设置了spring.datasource.enable.dynamic 等于true是开启多数据源
 */
@Configuration
@ConditionalOnProperty(name = {"spring.datasource.dynamic.enable"}, matchIfMissing = false, havingValue = "true")
public class DataSourceConfig {

    @Autowired
    private Environment env;

//	# Druid 数据源 1 配置，继承spring.datasource.druid.* 配置，相同则覆盖
//	...
//	spring.datasource.druid.one.max-active=10
//	spring.datasource.druid.one.max-wait=10000
//	...
//
//	# Druid 数据源 2 配置，继承spring.datasource.druid.* 配置，相同则覆盖
//	...
//	spring.datasource.druid.two.max-active=20
//	spring.datasource.druid.two.max-wait=20000
//	...
//	强烈注意：Spring Boot 2.X 版本不再支持配置继承，多数据源的话每个数据源的所有配置都需要单独配置，否则配置不会生效

    //	创建数据源
    //	所有引入db-core的模块都需要一个核心库，可以是user-center，也可以是oauth-center,file-center ,sms-center
    @Bean
    @ConfigurationProperties("spring.datasource.druid.core")
    public DataSource dataSourceCore() {
        return DruidDataSourceBuilder.create().build();
    }

//    //	所有的核心库共享一个日志中心模块，改模块不采用mysql中的innodb引擎，采用归档引擎
//    @Bean
//    @ConfigurationProperties("spring.datasource.druid.log")
//    public DataSource dataSourceLog() {
//        return DruidDataSourceBuilder.create().build();
//    }


    @Bean // 只需要纳入动态数据源到spring容器
    @Primary
    public DataSource dataSource() {
        DynamicDataSource dataSource = new DynamicDataSource();
        DataSource coreDataSource = this.dataSourceCore();
        //DataSource logDataSource = this.dataSourceLog();
        dataSource.addDataSource(DataSourceKey.core, coreDataSource);
        //dataSource.addDataSource(DataSourceKey.log, logDataSource);
        dataSource.setDefaultTargetDataSource(coreDataSource);
        return dataSource;
    }


    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") DataSource dataSource)
            throws Exception {
        MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(dataSource);
        String mapperLocations = "classpath*:com/open/**/mapper/mysql/*.xml";
        String db = env.getProperty("spring.datasource.druid.core.driver-class-name");
        if (db.contains("mysql")) {
            mapperLocations = "classpath*:com/open/**/mapper/mysql/*.xml";
        } else if (db.contains("kingbase")) {
            mapperLocations = "classpath*:com/open/**/mapper/kingbase/*.xml";
        } else if (db.contains("dm")) {
            mapperLocations = "classpath*:com/open/**/mapper/dm/*.xml";
        }else if (db.contains("sqlserver")) {
            mapperLocations = "classpath*:com/open/**/mapper/mssql/*.xml";
        }
        sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocations));
        MybatisConfiguration configuration = new MybatisConfiguration();
        // configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
        configuration.setLogImpl(org.apache.ibatis.logging.stdout.StdOutImpl.class);
        configuration.setMapUnderscoreToCamelCase(true);
        configuration.setCacheEnabled(false);
        sqlSessionFactory.setConfiguration(configuration);
        // sqlSessionFactory.setPlugins(new Interceptor[]{
        // //PerformanceInterceptor(),OptimisticLockerInterceptor()
        // paginationInterceptor() //添加分页功能
        // });
        // sqlSessionFactory.setGlobalConfig(globalConfiguration());
        return sqlSessionFactory.getObject();
    }


    @Bean // 将数据源纳入spring事物管理
    public DataSourceTransactionManager transactionManager(@Qualifier("dataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

}
